package br.ufmg.dcc.vod.remoteworkers.registry;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;

public class Lookup {

	public static <T extends Remote> T lookup(String host, int port, String objectName, Class<T> clazz) throws MalformedURLException, RemoteException, NotBoundException {
		String rmiUrl = "rmi://" + host + ":" + port + "/" + objectName;
		Remote lookup = Naming.lookup(rmiUrl);
		return clazz.cast(lookup);
	}

	public static <T extends Remote> T lookup(String url, String objectName, Class<T> clazz) throws MalformedURLException, RemoteException, NotBoundException {
		String[] split = url.split(":");
		
		if (split.length != 2) {
			throw new MalformedURLException("Malformed: " + url);
		}
		
		return lookup(split[0], split[1], clazz);
	}
	
}
